package com.ttj.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.ttj.domain.oracle.SysconfBranch;
import com.ttj.domain.oracle.WebUser;
import com.ttj.domain.oracle.WebValidationCode;
import com.ttj.mapper.oracle.SysconfBranchMapper;
import com.ttj.mapper.oracle.WebUserMapper;
import com.ttj.mapper.oracle.WebValidationCodeMapper;
import com.ttj.service.WebUserService;
import com.ttj.vo.BaseRes;

@Service
public class WebUserServiceImpl implements WebUserService {

	@Resource
	private WebUserMapper webUserMapper;

	@Resource
	private WebValidationCodeMapper codeMapper;

	@Resource
	private SysconfBranchMapper branchMapper;

	@Override
	public BaseRes<WebUser> createRecCheckUser(int type, String string) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		if (type == 1) {
			map.put("barcode", string);
		}
		if (type == 2) {
			map.put("phone", string);
		}
		WebUser user = webUserMapper.createRecCheckUser(map);
		BaseRes<WebUser> baseRes = new BaseRes<WebUser>();
		baseRes.setData(user);
		return baseRes;
	}

	@Override
	public BaseRes<WebUser> getUser(int type, String str) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		if (type == 1) {
			map.put("barcode", str);
		}
		if (type == 2) {
			map.put("phone", str);
		}
		WebUser user = webUserMapper.getUser(map);
		BaseRes<WebUser> baseRes = new BaseRes<WebUser>();
		baseRes.setData(user);
		return baseRes;
	}

	@Override
	public int updUser(double total, WebUser user, double orderWeight) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", 2);
		WebUser webUser = webUserMapper.getUser(map);
		webUser.setLastModifyTime(new Date());
		webUser.setVisualCoin(total);
		webUser.setCredit(user.getCredit() == null ? (double) 0 : user
				.getCredit());
		webUser.setSellnum(orderWeight);
		webUser.setPhonenum(user.getPhonenum());
		return webUserMapper.updUser(webUser);
	}

	@Override
	public Map<String, Object> register(WebUser user) {
		Map<String, Object> map = new HashMap<String, Object>();
		user.setTmcreateUser(user.getUsername());// 区域收购员
		user.setRegtype(3);// 注册类型
		Random r = new Random();
		int num = r.nextInt(89999)+10000;
		user.setPwd(num + "");// 默认密码
		user.setUsername(user.getPhonenum());// 默认用户名
		user.setName(user.getPhonenum());// 默认昵称
		user.setRegTime(new Date());// 注册时间
		user.setLastModifyTime(new Date());// 最后修改时间
		user.setVisualCoin(0.0);// 存款
		user.setCredit(0.0);// 绿猫币
		user.setTmcreateUser(user.getUsername());// 负责收购员
		user.setUserStatus(1);// 是否冻结
		user.setSellnum(0.0);// 累计回收量
		user.setType(1);// 个人客户
		String community = "";
		String[] split = user.getUseraddr().split("_");
		for (int i = 0; i < split.length; i++) {
			community = split[3];
		}
		SysconfBranch addrId = branchMapper.getAddrId(community);
		user.setAddrId(addrId.getAddrId());
		user.setBranchId(addrId.getId());
		int createUser = webUserMapper.createUser(user);

		if (createUser == 1) {
			map.put("res", "创建成功");
		} else {
			map.put("res", "创建失败");
		}
		return map;
	}

	@Override
	public int checkVerify(String phone, String verify, int type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phone", phone);
		map.put("verify", verify);
		map.put("type", type);
		List<WebValidationCode> list = codeMapper.checkVerify(map);
		return list.size();
	}

	@Override
	public int updUserAddr(String json) {
		Gson gson = new Gson();
		WebUser webUser = gson.fromJson(json, WebUser.class);
		webUser.setLastModifyTime(new Date());
		return webUserMapper.updUserAddr(webUser);
	}

}
